#!/usr/bin/env python 
# -*- coding:utf-8 -*-
'''
@File    :   data_export_process.py    
@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2022/4/24 0024 10:56   st      1.0         None
'''
import os
import openpyxl
from openpyxl.styles import Alignment

from utils.constent import LINE_CONNECTOR, matadata_dir, NGRAM_MAX, hownet_dir


def data_export_word_info(datas):
    """
    通过数据导出词组信息
    :param datas:
    :return:
    """
    try:
        # 创建一个Workbook对象
        wb = openpyxl.Workbook()
        # 添加一个sheet工作表、sheet名命名为Sheet1
        sheet = wb.create_sheet(index=0)
        table_head = ['词串', '词频', '互信息', '内部凝固度', '关联置信度', '左右信息熵', 'bert相似度平均值', 'houwnet相似度平均值', '汉典相似度平均值']
        for i, vlaue in enumerate(table_head):
            # 函数：table.write(行, 列, 要写入的数据)
            sheet.cell(1, i + 1, vlaue).alignment = Alignment(horizontal='left', vertical='center', wrapText=True)

        new_data =[]
        for data in datas:
            # id = data['id']
            # words_list = data['words_list']
            # text = ''.join(words_list)
            words_classify = data['words_classify']
            cla_high = words_classify['high']
            new_data.extend([(''.join(words), freq, pmi, pmi_mini, conf, H_min, sim_bert_aveg, sim_hownet_aveg, sim_handian_aveg) for words, K, (freq, pmi, pmi_mini, conf, H_min), (sim_bert_aveg, sim_hownet_aveg, sim_handian_aveg) in cla_high])
            cla_low = words_classify['low']
            new_data.extend([(''.join(words), freq, pmi, pmi_mini, conf, H_min, sim_bert_aveg, sim_hownet_aveg, sim_handian_aveg) for words, K, (freq, pmi, pmi_mini, conf, H_min), (sim_bert_aveg, sim_hownet_aveg, sim_handian_aveg) in cla_low])
            cla_none = words_classify['none']
            new_data.extend([(''.join(words), freq, pmi, pmi_mini, conf, H_min, sim_bert_aveg, sim_hownet_aveg, sim_handian_aveg) for words, K, (freq, pmi, pmi_mini, conf, H_min), (sim_bert_aveg, sim_hownet_aveg, sim_handian_aveg) in cla_none])
        for index, data in enumerate(new_data):
            for i, value in enumerate(data):
                # table.write(index+1, i, value)
                sheet.cell(index+2, i+1, value).alignment =Alignment(horizontal='center', vertical='center', wrapText=True)
        excel_file = os.path.join(matadata_dir, '肺炎_词组信息' + str(NGRAM_MAX) + '.xlsx')
        if os.path.exists(excel_file):
            os.remove(excel_file)
        wb.save(excel_file)
        print('结果保存Excel文本完成')
    except Exception as e:
        print(e)
        print('结果保存Excel文本失败')


def data_export_excel(datas):
    try:
        # 创建一个Workbook对象
        wb = openpyxl.Workbook()
        # 添加一个sheet工作表、sheet名命名为Sheet1
        sheet = wb.create_sheet(index=0)
        table_head = ['流水号', '文本', '高频：【词组】【K-N值】', '低频：【词组】【K-N值】', '无意义：【词组】【K-N值】']
        for i, vlaue in enumerate(table_head):
            # 函数：table.write(行, 列, 要写入的数据)
            sheet.cell(1, i+1, vlaue).alignment =Alignment(horizontal='left', vertical='center', wrapText=True)
            # table1.write(0, i, vlaue)
            # table2.write(0, i, vlaue)
            # table3.write(0, i, vlaue)

        new_datas = []
        for data in datas:
            id = data['id']
            words_list = data['words_list']
            text = ''.join(words_list)
            words_classify = data['words_classify']
            cla_high = words_classify['high']
            new_high = ['【' + ''.join(words) + '】' + '【' + str(K) + '】' for words, K, res, sims in cla_high]

            cla_low = words_classify['low']
            new_low = ['【' + ''.join(words) + '】' + '【' + str(K) + '】' for words, K, res, sims in cla_low]

            cla_none = words_classify['none']
            new_none = ['【' + ''.join(words) + '】' + '【' + str(K) + '】' for words, K, res, sims in cla_none]

            new_datas.append((id, text, '\n'.join(new_high), '\n'.join(new_low), '\n'.join(new_none)))
        for index, data in enumerate(new_datas):
            for i, value in enumerate(data):
                # table.write(index+1, i, value)
                sheet.cell(index+2, i+1, value).alignment =Alignment(horizontal='left', vertical='center', wrapText=True)
        excel_file = os.path.join(matadata_dir, 'matadata_result.xlsx')
        if os.path.exists(excel_file):
            os.remove(excel_file)
        wb.save(excel_file)
        print('结果保存Excel文本完成')
    except Exception as e:
        print(e)
        print('结果保存Excel文本失败')


def data_export_txt(datas):
    """
    数据存储到txt
    :param datas:
    :return:
    """
    f_high = open(os.path.join(matadata_dir, 'matadata_high_score.txt'), 'w', encoding='utf-8')
    f_high.truncate()
    f_high.write(
        'id' + LINE_CONNECTOR + 'words_list' + LINE_CONNECTOR + 'word' + LINE_CONNECTOR + 'K' + LINE_CONNECTOR + 'res' + LINE_CONNECTOR + '\n')
    f_low = open(os.path.join(matadata_dir, 'matadata_low_score.txt'), 'w', encoding='utf-8')
    f_low.truncate()
    f_low.write(
        'id' + LINE_CONNECTOR + 'words_list' + LINE_CONNECTOR + 'words' + LINE_CONNECTOR + 'K' + LINE_CONNECTOR + 'res' + LINE_CONNECTOR + '\n')
    f_none = open(os.path.join(matadata_dir, 'matadata_none_score.txt'), 'w', encoding='utf-8')
    f_none.truncate()
    f_none.write(
        'id' + LINE_CONNECTOR + 'words_list' + LINE_CONNECTOR + 'word' + LINE_CONNECTOR + 'K' + LINE_CONNECTOR + 'res' + LINE_CONNECTOR + '\n')

    for data in datas:
        id = data['id']
        words_list = data['words_list']
        words_classify = data['words_classify']

        cla_high = words_classify['high']
        for words, K, res, sims in cla_high:
            f_high.write(id + LINE_CONNECTOR + str(words_list) + LINE_CONNECTOR + str(words) + LINE_CONNECTOR + str(
                K) + LINE_CONNECTOR + str(res) + '\n')
        cla_high = words_classify['low']
        for words, N, res, sims in cla_high:
            f_low.write(id + LINE_CONNECTOR + str(words_list) + LINE_CONNECTOR + str(words) + LINE_CONNECTOR + str(
                N) + LINE_CONNECTOR + str(res) + '\n')
        cla_high = words_classify['none']
        for words, K, res, sims in cla_high:
            f_none.write(id + LINE_CONNECTOR + str(words_list) + LINE_CONNECTOR + str(words) + LINE_CONNECTOR + str(
                K) + LINE_CONNECTOR + str(res) + '\n')
    print('结果保存txt文本完成')


def data_export_hownet(all_senses, s_dict):
    """
    义原数据导出excel
    :param all_senses:
    :param s_dict:
    :return:
    """
    # 创建一个Workbook对象
    wb = openpyxl.Workbook()
    # 添加一个sheet工作表、sheet名命名为Sheet1
    sheet = wb.create_sheet(index=0)

    table_head = ['id', '中文词语', '中文词语词性', '中文词语情感倾向', '英文词语', '英文词语词性', '英文词语情感倾向', '义原标注', 'RMK']
    for i, vlaue in enumerate(table_head):
        # 函数：table.write(行, 列, 要写入的数据)
        sheet.cell(1, i + 1, vlaue).alignment = Alignment(horizontal='left', vertical='center', wrapText=True)

    for index, senses in enumerate(all_senses):
        (s_c, s_e) = s_dict.get(senses.No) if s_dict.__contains__(senses.No) else ('', '')
        sheet.cell(index + 2, 1, senses.No).alignment = Alignment(horizontal='center', vertical='center', wrapText=True)
        sheet.cell(index + 2, 2, senses.zh_word).alignment = Alignment(horizontal='center', vertical='center', wrapText=True)
        sheet.cell(index + 2, 3, senses.zh_grammar).alignment = Alignment(horizontal='center', vertical='center', wrapText=True)
        sheet.cell(index + 2, 4, s_c).alignment = Alignment(horizontal='center', vertical='center', wrapText=True)
        sheet.cell(index + 2, 5, senses.en_word).alignment = Alignment(horizontal='center', vertical='center', wrapText=True)
        sheet.cell(index + 2, 6, senses.en_grammar).alignment = Alignment(horizontal='center', vertical='center', wrapText=True)
        sheet.cell(index + 2, 7, s_e).alignment = Alignment(horizontal='center', vertical='center', wrapText=True)
        sheet.cell(index + 2, 8, str(senses.get_sememe_list())).alignment = Alignment(horizontal='center', vertical='center', wrapText=True)
        sheet.cell(index + 2, 9, senses.Def).alignment = Alignment(horizontal='center', vertical='center', wrapText=True)

    excel_file = os.path.join(hownet_dir, '义原信息表.xlsx')
    if os.path.exists(excel_file):
        os.remove(excel_file)
    wb.save(excel_file)
    print('结果保存Excel文本完成')